package com.dpj.aqs;

import lombok.extern.slf4j.Slf4j;

import javax.lang.model.type.ExecutableType;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//CountDownLatch 无法无法重复使用，当创建的计时数使用完之后，就需要重新创建CountDownLatch对象
// 用cyclicbarrier可以解决这个问题
@Slf4j(topic = "c.TestCountDownLatchProblem")
public class TestCountDownLatchProblem {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService pool = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 3; i++) {
            CountDownLatch latch=new CountDownLatch(2);
            pool.submit(()->{
                log.debug("task1 start");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                latch.countDown();
            });
            pool.submit(()->{
                log.debug("task2 start");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                latch.countDown();
            });

            latch.await();
            log.debug("task1 task2 finished");
        }

        pool.shutdown();
    }
}
